/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rafkind.crawl.generator;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author drafkind
 */
public class Main {

  /**
   * @param args the command line arguments
   */
  public static class Monster {
    private String type;
    private String name;
    private String color;
    private int power;
    private int att;
    private int def;

    public Monster(String type, String props) {
      this.type = type;

      String[] s = props.split(",");

      name = s[0];

      color = s[1].trim().substring(0, 1);
      power = Integer.parseInt(s[1].trim().substring(1, 2));
    }

    public String getName() { return name; }

    public String getColor() { return color; }
    
    public int getPower() { return power; }

    public void printAsXml() {
      System.out.println(
          MessageFormat.format(
            "    <type name=\"{0}\" color=\"{1}\" level=\"{2}\" />",
            name,
            color,
            power));
    }
  }

  public static class Group {
    private Map<Integer, Monster> howManyOfEach;

    public static List<Group> createGroup(List<Monster> monsters, int targetAttack, int targetDefence) {
      /* */
      return null;
    }

    public void print() {
      boolean b = false;
      for (Map.Entry<Integer, Monster> e : howManyOfEach.entrySet()) {
        if (b) {
          System.out.print(", ");
        } else {
          b = true;
        }
        System.out.print(e.getKey() + " x " + e.getValue().getName());
      }
      System.out.println();
    }
  }

  public static class SubArea {
    private List<Monster> monsters;
    private List<Group> groups;

    public SubArea() {
      monsters = new ArrayList<Monster>();
      groups = new ArrayList<Group>();
    }
  }

  public static void main(String[] args) {
    String[][] table = new String[][]{
      {"goblin","angry goblin, R1","goblin soldier, R2","goblin ringleader, R3","goblin rogue, R4"},
{"elf","leprechaun, G1","meadow elf, W3","drow, G5","Alberich, W7"},
{"dwarf","miner, R3","forger, R4","dwarf soldier, R5","dwarrowdelf, R6"},
{"orc","orc, R4","orc battler, R5","orc mage, R6","melko, R7"},
{"human","rogue, W2","fighter, W4","wizard, W6","politician, W8"},
{"ogre","gray ogre, R2","mean ogre, R3","doomed ogre, R4","ridiculous ogre, R5"},
{"stick guy","walking stick, G1","vineman, G3","strawman, G5","pulper, G7"},
{"flower guy","flowerman, G2","flower soldier, G4","flower captain, W6","doom flower, W8"},
{"mushroom","evil mushroom, G3","sporecap, G5","death angel, G7","shiitake, G9"},
{"tree","treefolk, G3","treefolk warrior, G5","treefolk mage, U7","redwood, R9"},
{"carnivorous plants","carnivorous plant, G2","chomper, G4","venus flytrap, G6","audrey, G8"},
{"seed","vibrating seed, W3","hard seed, W4","tough seed, G5","gold seed, G6"},
{"amphibian","frog, G1","rock frog, R2","poison frog, G5","blood frog, R6"},
{"reptile","gecko, R2","lizard, G3","crocodile, U7","komodo, G8"},
{"dog","hound, G1","wolf, G2","wolverine, R5","blink dog, U9"},
{"cat","puma, G3","sabretooth, W5","panther, G7","mountain lion, G9"},
{"fish","biter, U5","icthy, U6","silverscale, U7","rumblefish, U8"},
{"tentacled","octopus, U4","gripper, U5","inkshot, U6","kraken, U9"},
{"eye","orbus, U3","oculus, W5","bloodshot, R7","Orms-by-gore, B9"},
{"brain","brain, U6","thinker, U7","pulsator, U8","pure intellect, U9"},
{"hand","swift hand, W2","touch of evil, B4","manos, B6","hand of fate, B8"},
{"mouth","old yeller, W3","bigmouth, G4","cheshire, U5","rocky horror, B6"},
{"head","miles ahead, U6","head of the class, U7","headron, U8","hedly, U9"},
{"beholder","green beholder, G6","red beholder, R7","blue beholder, U8","black beholder, B9"},
{"crow","storm crow, U3","fire crow, R4","painted crow, U5","Aesop, W6"},
{"hawk","grey hawk, W2","divebomber, W3","falcon, W4","thunderbird, R5"},
{"griffin","griffin, W2","griffin elder, W4","ancient griffin, W6","eternal griffin, W8"},
{"vulture","vulture, B3","scavenger, B4","putrid vulture, B5","raptor, B6"},
{"phoenix","new phoenix, R4","firestarter, R5","arisen, R6","sharding pheonix, R7"},
{"owl","silent owl, W2","unwise owl, W3","masked owl, W4","great owl, U5"},
{"mosquito","mosquito, G1","drinker, G2","energy mosquito, R3","drainer, B4"},
{"bee","honeybee, G3","firebee, R4","ice wasp, U5","spectral bee, B6"},
{"fly","robber fly, B2","rogue fly, B4","robber baron fly, B5","banker fly, B6"},
{"dragonfly","dragonfly, G4","needler, G5","crystal fly, U6","glasswing, U7"},
{"butterfly","butterfly, W2","moth, W3","monarch, W4","swallowtail, W5"},
{"spider","minor spider, G1","major spider, G2","black widow, B3","tarantula, B4"},
{"skeleton","skeleton, B1","evil bones, B3","all that's left, B5","death rattle, B7"},
{"zombie","zombie, B2","braineater, B4","accursed wanderer, B6","restless, B8"},
{"vampire","vampire, B5","vampire lord, B6","vampire mage, B7","vampire king, B8"},
{"mummy","lesser mummy, B4","greater mummy, B5","pharao, W6","jewel hoarder, W7"},
{"ghost","ghost, B3","apparition, B4","soul, B5","ghoul, B6"},
{"baby dragon","baby red, R4","baby white, W4","baby blue, U5","baby black, B5"},
{"4 leg dragon","fire dragon, R5","ice dragon, U6","acid dragon, G6","lightning dragon, W7"},
{"2 leg dragon","dragon gatekeeper, G6","dragon enforcer, R6","dragon lord, W8","dragon mage, U9"},
{"flying dragon","death ray dragon, B9","crystal dragon, U9","burrowing dragon, R8","jungle dragon, G8"},
{"dragon skeleton","dragon skeleton, B7","decaying husk, B8","dragon simalcrum, U8","dragon lich, B9"},
{"wyrm","wyrm, G5","poisoner, G6","wyrm queen, R7","wyvern, B7"},
{"book","knowledge, U4","strategy, U5","power, U6","trickery, U7"},
{"door","barrier, U3","gate, B4","portal, U5","vault protector, W6"},
{"candle","candle, R3","unholy flame, B4","fires of vengeance, R5","deadlight, B6"},
{"sword","flying sword, W3","angry machete, W4","darting rapier, W5","pointed guardian, W6"},
{"suit of armor","copper armor, W4","iron armor, W5","steel armor, W6","kevlar, W7"},
{"golem","mud golem, R2","stone golem, R4","steel golem, U6","silver golem, W8"},
{"slime","green slime, G1","blue slime, U2","purple slime, R3","yellow slime, W4"},
{"cloud","cloud, U3","smoke, R4","gas, G5","vapor, B6"},
{"wave","wave, U3","water elemental, U4","rogue wave, U5","tsunami, U6"},
{"flame","flame, R4","fire elemental, R5","sparky, R6","conflagrator, R7"},
{"ooze","dirty puddle, W3","acid ooze, G5","creeping death, B7","the blob, G9"},
{"elemental humanoid","nature elemental, G5","earth elemental, R6","wind elemental, U7","light elemental, W8"}

    };

    ArrayList<Monster> monsters = new ArrayList<Monster>();
    System.out.println("<monsterTypes>");
    for (String[] m : table) {
      System.out.println("  <superType name=\"" + m[0] + "\">");
      for (int i = 1; i < m.length; i++) {
        Monster mon = new Monster(m[0], m[i]);
        mon.printAsXml();
        monsters.add(mon);

      }
      System.out.println("  </superType>");
    }
    System.out.println("</monsterTypes>");
    System.exit(0);

    Collections.sort(monsters, new Comparator<Monster>(){
      public int compare(Monster o1, Monster o2) {
        int c = o1.getColor().compareTo(o2.getColor());
        if (c == 0)
          c = o1.getPower() - o2.getPower();
        if (c == 0)
          c = o1.getName().hashCode() - o2.getName().hashCode();
        return c;
      }

    });

    Map<String, Integer> counts = new HashMap<String, Integer>();
    Map<String, List<Monster>> byColor = new HashMap<String, List<Monster>>();
    
    for (Monster mo: monsters) {
      //System.out.println(mo.getColor() + mo.getPower() + " - " + mo.getName());
      Integer count = counts.get(mo.getColor());
      if (count == null) {
        count = 1;
      } else {
        count += 1;
      }
      counts.put(mo.getColor(), count);

      List<Monster> c = byColor.get(mo.getColor());
      if (c == null) {
        c = new ArrayList<Monster>();
        byColor.put(mo.getColor(), c);
      }
      c.add(mo);
    }

    

    String[][] colorChoices = new String[][] {
      {"G", "W", "R"},
      {"R", "G", "B"},
      {"U", "W", "B"},
      {"W", "U", "G"},
      {"B", "R", "U"},
      {"B", "U", "R"},
      {"W", "G", "U"},
      {"U", "B", "W"},
      {"R", "B", "G"},
      {"G", "R", "W"}
    };

    List<List<List<Monster>>> levels = new ArrayList<List<List<Monster>>>();
    for (String[] colorChoice : colorChoices) {
      List<Monster> main = byColor.get(colorChoice[0]);
      List<Monster> side1 = byColor.get(colorChoice[1]);
      List<Monster> side2 = byColor.get(colorChoice[2]);

      List<List<Monster>> area = new ArrayList<List<Monster>>();

      // add 7 of base
      List<Monster> easy = new ArrayList<Monster>();
      for (int i = 0; i < 7; i++)
        easy.add(main.remove(0));
      area.add(easy);

      // med = 4 base + 3 side
      List<Monster> med = new ArrayList<Monster>();
      for (int i = 0; i < 4; i++)
        med.add(main.remove(0));
      for (int i = 0; i < 3; i++)
        med.add(side1.remove(0));
      area.add(med);

      // hard - 4 base + 3 side
      List<Monster> hard = new ArrayList<Monster>();
      for (int i = 0; i < 4; i++)
        hard.add(main.remove(0));
      for (int i = 0; i < 3; i++)
        hard.add(side2.remove(0));
      area.add(hard);

      levels.add(area);
    }

    int a = 1;
    int targetAttack = 60;
    int targetDefence = 20;
    int count = 0;
    for (List<List<Monster>> level : levels) {
      System.out.println("Area " + (a++));
      int s = 1;
      for (List<Monster> sub : level) {
        System.out.println("  Subarea " + (s++));

        targetAttack += ((count / 4)+1) * 30;
        targetDefence += ((count / 4)+1) * 10;

        List<Group> g = Group.createGroup(sub, targetAttack, targetDefence);

        count += 2;
        for (Monster m : sub) {
          System.out.println("    " + m.getColor() + m.getPower() + " " + m.getName());
        }

        /*for (Group gr : g) {
          gr.print();
        }*/
      }
      
    }

    for (Map.Entry<String, List<Monster>> e : byColor.entrySet()) {
      System.out.println(e.getKey());
      for (Monster m : e.getValue()) {
        System.out.println("  " + m.getPower() + " - " + m.getName());
      }
    }/*

    for (Map.Entry<String, Integer> e : counts.entrySet()) {
      System.out.println(e.getKey() + " = " + e.getValue());
    }*/
  }
}
